var baseURL = process.env.BASE_API
import store from "@/store";

export default function ({url, method, data, params, uploadFile, headers, processData}) {
    return new Promise((resolve, reject) => {
        var d = data || params || {}
        // d._t = new Date()
        if(uploadFile){
            wx.uploadFile({
                url: baseURL + url,
                filePath: data.file.path,
                name: 'file',
                success (res){
                    resolve(res.data)
                },
                fail(res){
                    wx.showToast({
                        title: '上传失败',
                        icon: 'none',
                        duration: 2000
                    })
                    reject(res.data);
                },
            })
            return
        }
        
        if(headers){
            for(var k in headers){
                request.setRequestHeader(k, headers[k])
            }
        } else {
            headers = {}
        }
        
        if(store.getters.userClient==2){
            if(store.getters.user2&&store.getters.user2.accessToken){
                headers.token = store.getters.user2.accessToken
            }
        } else if(store.getters.userClient==3){
            if(store.getters.user3&&store.getters.user3.accessToken){
                headers.token = store.getters.user3.accessToken
            }
        } else {
            if(store.getters.token){
                // debug
                headers.token = store.getters.token
            }
        }
        

        wx.request({
            url: baseURL+ url,
            data: d,
            method: method,
            header: headers || {},
            success (res) {
                var data = res.data
                if(data.code != 0){
                    wx.showToast({
                        title: data.message,
                        icon: 'none',
                        duration: 2000
                    })
                } else {
                    resolve(data)
                }
            },
            fail(res){
                console.log(res)
                wx.showToast({
                    title: '网络出现问题',
                    icon: 'none',
                    duration: 2000
                })
                reject(res.data);
            },
            complete(res){

            }
        })
    })
};